package com.googlecode.bip.xml.importe;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.googlecode.bip.entity.IllegalValueException;

/**
 * Handle XML import to MySQL database
 */
// not public
class SQLTopic  extends Commiter implements Commitable
{
    private static final Logger logger = Logger.getLogger( SurveyScanner.class );

    private Integer idtopic;
    private SQLTopic parentSQLTopic;
    private Integer order;
    private Integer isactive;

    private List<SQLTopic> childSQLTopicList = new ArrayList<SQLTopic>();
    private List<SQLTopicLocal> childSQLTopicLocalList = new ArrayList<SQLTopicLocal>();
    private List<SQLQuestion> childSQLQuestionList = new ArrayList<SQLQuestion>();

    SQLTopic( SQLStatementCollection sqlCollection )
    {
        super( sqlCollection );

        if( logger.isDebugEnabled() ) {
            logger.debug( "NEW SQLTopic() " + super.toString() );
            }
    }

    void setIdTopic( int idtopic )
    {
        if( idtopic == 0 ) {
            throw new IllegalArgumentException( "idtopic=0 not allowed" );
            }
        this.idtopic = idtopic;
    }

    int getIdTopic()
    {
        return this.idtopic; // check is not null
    }

    static String getIdTopicSQLString( SQLTopic parentSQLTopic )
    {
        if( parentSQLTopic == null ) {
            return "NULL";
            }
        else {
            return Integer.toString( parentSQLTopic.getIdTopic() );
            }
    }

    void setParent( SQLTopic parent )
    {
        if( parent == null ) {
            // No reason to set null, null for getParent() is allowed
            throw new NullPointerException( "parent SQLTopic is null" );
            }

        this.parentSQLTopic = parent;
    }

    SQLTopic getParent()
    {
        return this.parentSQLTopic;
    }

    void setOrder( int order )
    {
        this.order = order;
    }

    void setIsActive( int isactive )
    {
        this.isactive = isactive;
    }

    void add( SQLTopic child )
    {
        if( child == null ) {
            throw new NullPointerException("child Topic is null");
            }
        childSQLTopicList.add( child );
    }

    void add( SQLTopicLocal child )
    {
        if( child == null ) {
            throw new NullPointerException("child SQLTopicLocal is null");
            }
        childSQLTopicLocalList.add( child );
    }

    void add( SQLQuestion child )
    {
        if( child == null ) {
            throw new NullPointerException("child SQLQuestion is null");
            }
        childSQLQuestionList.add( child );
    }

    @Override
    public void commit() throws NamingException, SQLException, IllegalValueException
    {
        if( idtopic == null ) {
            throw new IllegalStateException( "idtopic is null" + this );
            }
        // parentidtopic could be null (root Topic)
        if( order == null ) {
            throw new IllegalStateException( "order is null" );
            }
        if( isactive == null ) {
            throw new IllegalStateException( "isactive is null" );
            }

//        String sql = "REPLACE INTO `topics` (`idtopic`, `parentidtopic`, `order`, `isactive`) VALUES ('"
//            + idtopic
//            + "',"
//            + getIdTopicSQLString( this.parentSQLTopic )
//            + ",'"
//            + order
//            + "','"
//            + isactive
//            + "');";
        String parentIdTopicStr = getIdTopicSQLString( this.parentSQLTopic );
        String sql = "INSERT INTO `topics` (`idtopic`, `parentidtopic`, `order`, `isactive`) VALUES ('"
            + idtopic
            + "',"
            + parentIdTopicStr
            + ",'"
            + order
            + "','"
            + isactive
            + "') ON DUPLICATE KEY UPDATE `parentidtopic`="
            + parentIdTopicStr
            + ", `order`='"
            + order
            + "', `isactive`='"
            + isactive
            + "';";

        super.addSQLStatement( sql );

        for( SQLTopicLocal c : childSQLTopicLocalList ) {
            c.setParent( this );
            c.commit();
            }

        for( SQLTopic c : childSQLTopicList ) {
            c.setParent( this );
            c.commit();
            }

        for( SQLQuestion c : childSQLQuestionList ) {
            c.setParent( this );
            c.commit();
            }
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(  super.toString() )
               .append(" [idtopic=" ).append( idtopic )
               .append( ", getIdTopicSQLString=" ).append( getIdTopicSQLString( parentSQLTopic ) )
               .append( ", order=" ).append( order )
               .append( ", isactive=" ).append( isactive )
               //.append( ", childSQLTopicList=" ).append( childSQLTopicList )
               //.append( ", childSQLTopicLocalList=" ).append( childSQLTopicLocalList )
               //.append( ", childSQLQuestionList=" ).append( childSQLQuestionList )
               .append( "]" );
        return builder.toString();
    }

}
